<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Course;
use app\admin\model\Coursetype;
use app\admin\model\Naturetype;
use app\admin\model\News;
use app\admin\model\Newstype;
use app\admin\model\Slideshow;
use think\Request;
use think\File;
class  Nature extends Base
{
    //资讯分类列表
    public function type_list(){
        $user = $this->user_info();
        $resource = collection(Naturetype::order("sort asc")->select())->toArray();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$resource,
        );
        return view("typelist",$data);
    }
    //添加
    public function type_addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("type_addform",$data);
    }
    public function type_add(Request $request){
        //接收所有的请求数据，数组：多个数据的集合
        $data = $request->post();
        // 创建数组元素
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = Naturetype::insert($data);  //执行数据库命令
        //返回结果的判断
        if($res){
            $this->success("添加成功","Nature/type_list");
        }else{
            $this->error("添加失败","Nature/type_addform");
        }
    }

    //删除分类
    public function type_deletes(Request $request){
        $user = $this->user_info();
        $id = $request->post('id');
        if(Naturetype::destroy($id)){
            $this->success("删除成功","Nature/type_list");
        }else{
            $this->error("删除失败");
        }
    }
    //修改分类状态
    public function setTypeStatus(Request $request)
    {
        $user = $this->user_info();
        if (Naturetype::update($request->post())) {
            $this->success("成功", "Nature/type_list");
        } else {
            $this->error("失败");
        }
    }

    //修改分类
    public function type_update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Naturetype::where(array("id"=>$id))->find(),
        );
        return view("type_update",$data);
    }
    public function type_toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(Naturetype::update($data)){
            $this->success("修改成功","Nature/type_list");
        }else{
            $this->error("修改失败");
        }
    }

}
